package com.mj.mafeed.service;

import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;

import com.mj.mafeed.bean.ProductListJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.util.HttpUtils;
import com.mj.mafeed.util.SPUtils;

/**
 * 我的关注
 * @author zhaominglei
 * @date 2014-12-29
 * 
 */
public class FavoriteService extends BaseService {
	/**
	 * 我的关注列表
	 */
	public ProductListJSON getFavorite(String referer, String username, String sign) {
		StringBuilder url = new StringBuilder(Constants.MEMBER_FAVORITE_URL);
		url.append("?username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doGet(url.toString());
		if (result != null && !result.equals("")) {
			try {
				ProductListJSON productListJSON = new ObjectMapper().readValue(result, ProductListJSON.class);
				return (productListJSON != null) ? productListJSON : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 添加我的关注
	 */
	public boolean addFavorite(Context context, String referer, Long productId) {
		String username = (String)SPUtils.get(context, "username", "");
		String sign = (String)SPUtils.get(context, "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("") 
				&& productId != null && productId.longValue() > 0l) {
			StringBuilder param = new StringBuilder();
			param.append("username=").append(username).append("&sign=").append(sign);
			param.append("&productId=").append(productId);
			String result = HttpUtils.doPost(Constants.MEMBER_FAVORITE_ADD_URL, param.toString());
			if (result != null && result.equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 删除我的关注
	 */
	public boolean deletFavorite(Context context, String referer, Long productId) {
		String username = (String)SPUtils.get(context, "username", "");
		String sign = (String)SPUtils.get(context, "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("") 
				&& productId != null && productId.longValue() > 0l) {
			StringBuilder param = new StringBuilder();
			param.append("username=").append(username).append("&sign=").append(sign);
			param.append("&productId=").append(productId);
			String result = HttpUtils.doPost(Constants.MEMBER_FAVORITE_DELETE_URL, param.toString());
			if (result != null && result.equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 检查是否已关注
	 */
	public boolean checkFavorite(Context context, String referer, Long productId) {
		String username = (String)SPUtils.get(context, "username", "");
		String sign = (String)SPUtils.get(context, "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("") 
				&& productId != null && productId.longValue() > 0l) {
			StringBuilder param = new StringBuilder();
			param.append("username=").append(username).append("&sign=").append(sign);
			param.append("&productId=").append(productId);
			String result = HttpUtils.doPost(Constants.MEMBER_FAVORITE_CHECK_URL, param.toString());
			if (result != null && result.equals("true")) {
				return true;
			}
		}
		return false;
	}
}
